package util;

import java.sql.*;

public class JDBCUtil {
    private static  String diverName = "com.mysql.cj.jdbc.Driver";
    private static  String url = "jdbc:mysql://localhost:3306/dormitory?serverTimezone=GMT%2B8";
    private static  String user = "root";
    private static  String password = "123456";

    static {
        try {
            Class.forName(diverName);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    public static  void release(Connection connection, Statement statement, ResultSet resultSet){

            try {
                if (connection != null) {
                    connection.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (resultSet !=null){
                    resultSet.close();
                }
            }catch (SQLException e) {
                throw new RuntimeException(e);
            }
    }

    public static void main(String[] args) {
        System.out.println(JDBCUtil.getConnection());
    }
}
